﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class MediaFileTests
    {

        [TestMethod]
        public void Clone()
        {
            MediaFolder folder = new MediaFolder();
            Site site = new Site();
            MediaFile original = new MediaFile
                                     {
                                         ID = 1,
                                         Name = "picture.png",
                                         StartPublishing = new DateTime(2010, 9, 1),
                                         EndPublishing = new DateTime(2010, 12, 31),
                                         Extension = "png",
                                         Folder = folder,
                                         Site = site,
                                         Size = 1000,
                                         SortKey = "10"
                                     };
            MediaFile clone = (MediaFile) original.Clone();
            Assert.AreEqual(0, clone.ID);
            Assert.IsTrue(TestUtils.DateTimeValidation.IsNow(clone.UploadedTimestamp));
            Assert.IsTrue(TestUtils.DateTimeValidation.IsNow(long.Parse(clone.Version)));
            Assert.AreEqual("picture.png", clone.Name);
            Assert.AreNotSame(folder, clone.Folder);
            Assert.AreSame(site, clone.Site);
            Assert.IsTrue(site.MediaFiles.Contains(clone));
            Assert.AreEqual("png", clone.Extension);
            Assert.AreEqual(1000, clone.Size);
            Assert.AreEqual(new DateTime(2010, 9, 1), clone.StartPublishing);
            Assert.AreEqual(new DateTime(2010, 12, 31), clone.EndPublishing);
            Assert.AreEqual("10", clone.SortKey);
        }


        private MediaFile SetupFile()
        {
            Site site = new Site { RootMediaFolder = "media" };
            MediaFolder a = new MediaFolder { Site = site, Name = "a" };
            MediaFolder b = new MediaFolder { Site = site, Name = "b", Parent = a };
            MediaFolder c = new MediaFolder { Site = site, Name = "c", Parent = b };
            MediaFile file = new MediaFile { Site = site, Name = "picture.png", Folder = c };
            return file;
        }


        [TestMethod]
        public void GetVirtualPath()
        {
            Assert.AreEqual("media/a/b/c/picture.png", SetupFile().GetVirtualPath());
        }


        [TestMethod]
        public void GetPhysicalPath()
        {
            Assert.AreEqual(@"media\a\b\c\picture.png", SetupFile().GetPhysicalPath());
        }


        [TestMethod]
        public void GetAllCategories_BothFileAndFolderHasCategories_AllCategoriesAreReturned()
        {
            MediaFolder f1 = new MediaFolder() { ID = 1 };
            MediaFolder f2 = new MediaFolder() { ID = 2 };
            MediaFile p = new MediaFile { Folder = f2 };
            f2.Files.Add(p);
            f1.SubFolders.Add(f2);
            f2.Parent = f1;
            Category pcVehicles = new Category() { ID = 1, Name = "Vehicles" };
            Category pcCars = new Category() { ID = 2, Name = "Cars" };
            Category pcVolvo = new Category() { ID = 3, Name = "Volvo" };
            p.Categories.Add(pcVolvo);
            f1.Categories.Add(pcVehicles);
            f2.Categories.Add(pcCars);
            // Check that the correct number of categories is returned
            Assert.AreEqual(3, p.GetAllCategories().Count);
            // Check that they are returned in the correct order, starting with the page and then upwards in the folder hierarchy
            Assert.AreSame(pcVolvo, p.GetAllCategories()[0]);
            Assert.AreSame(pcCars, p.GetAllCategories()[1]);
            Assert.AreSame(pcVehicles, p.GetAllCategories()[2]);
        }


        [TestMethod]
        public void GetCategoryNames_BothPageAndFolderHasCategories_StringWithVolvoCarsVehicles()
        {
            MediaFolder f1 = new MediaFolder() { ID = 1 };
            MediaFolder f2 = new MediaFolder() { ID = 2 };
            MediaFile p = new MediaFile { Folder = f2 };
            f2.Files.Add(p);
            f1.SubFolders.Add(f2);
            f2.Parent = f1;
            Category pcVehicles = new Category() { ID = 1, Name = "Vehicles" };
            Category pcCars = new Category() { ID = 2, Name = "Cars" };
            Category pcVolvo = new Category() { ID = 3, Name = "Volvo" };
            p.Categories.Add(pcVolvo);
            f1.Categories.Add(pcVehicles);
            f2.Categories.Add(pcCars);
            Assert.AreEqual("Volvo, Cars, Vehicles", string.Join(", ", p.GetCategoryNames()));
        }

    }

}
